package com.fitness.front.aop.real;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.fitness.front.aop.LogMessage;
import com.fitness.front.modal.course.vo.CourseVO;
import com.fitness.front.modal.datavisual.MemberBehavior;
import com.fitness.front.modal.resource.entity.Venue;
import com.fitness.front.modal.response.ServerResponseEntity;
import com.fitness.front.service.IVenueService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
@Aspect
@Slf4j
public class CourseVenueAspect {

    @Resource
    private IVenueService venueService;

    @Pointcut("execution(* com.fitness.front.controller.VenueController.venueSign*(..))")
    public void venuePointcut() {
    }

    @Around("venuePointcut()")
    public Object venuePointcutLog(ProceedingJoinPoint point) throws Throwable{
        try {
            Long venueId = (Long)(point.getArgs()[0]);
            Venue venue = venueService.getById(venueId);
            String method = point.getSignature().getName();
            Object result = point.proceed();
            Map<String,Object> venueLog = new HashMap<>();
            venueLog.put("venueId",venueId);
            venueLog.put("venueName",venue.getName());
            LogMessage<Map<String,Object>> logMessage = new LogMessage<>(DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"),
                    method.contains("venueSignIn") ? LogMessage.VENUE_SIGN_IN_TYPE : LogMessage.VENUE_SIGN_OUT_TYPE, venueLog);
            log.info("{}", JSONUtil.toJsonStr(logMessage));
            return result;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }
}
